package com.gps.skyrc.tool;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.gps.skyrc.R;
import com.gps.skyrc.adapter.ShowNameAdapter;
import com.gps.skyrc.bean.DeviceInfo;
import com.gps.skyrc.port.OnlyClickListener;

import java.util.List;
import java.util.Objects;

/**
 * Create by ywq
 * data 2018/12/4
 */
public class ShowNameDialog extends Dialog {

    private TextView mCancel;
    private RecyclerView mRecycle;
    private List<DeviceInfo> mInfos;
    private Context mContext;
    private onDeviceChooseClickListener mOnDeviceChooseClickListener;
    private ShowNameAdapter mShowNameAdapter;

    public ShowNameDialog(@NonNull Context context, List<DeviceInfo> infos) {
        super(context);
        this.mContext = context;
        this.mInfos = infos;
    }

    public void setDeviceChooseClickListener(onDeviceChooseClickListener onDeviceChooseClickListener) {
        this.mOnDeviceChooseClickListener = onDeviceChooseClickListener;
    }

    public interface onDeviceChooseClickListener {
        void onCancel();

        void onItemClick(DeviceInfo info, int position);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Objects.requireNonNull(getWindow()).setBackgroundDrawable(new BitmapDrawable());
        setContentView(R.layout.showname_dialog);
        //按空白处不能取消动画
        setCanceledOnTouchOutside(false);
        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();
    }

    private void initView() {
        mCancel = findViewById(R.id.cancel);
        mRecycle = findViewById(R.id.recycle);
    }

    private void initData() {
        mShowNameAdapter = new ShowNameAdapter(mContext, mInfos, mListener);
        mRecycle.setLayoutManager(new LinearLayoutManager(mContext));
        mRecycle.setAdapter(mShowNameAdapter);
    }

    OnlyClickListener mListener = new OnlyClickListener() {
        @Override
        public void onItemClick(DeviceInfo info, int position) {
            if (mOnDeviceChooseClickListener != null)
                mOnDeviceChooseClickListener.onItemClick(info, position);
        }
    };

    private void initEvent() {
        mCancel.setOnClickListener(v -> {
            if (mOnDeviceChooseClickListener != null)
                mOnDeviceChooseClickListener.onCancel();
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, @NonNull KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }
}
